﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.ComponentModel;

namespace PhoneAppSTS.Utils
{
    public class STSSettings : INotifyPropertyChanged
    {
        private IsolatedStorageSettings settings;

        readonly string BeginTimeSettingKeyName = "BeginTimeSetting";
        readonly string MiddleTimeSettingKeyName = "MiddleTimeSetting";
        readonly string LastTimeSettingKeyName = "LastTimeSetting";

        readonly string BeginIsCheckedSettingKeyName = "BeginIsCheckedSettingKeyName";
        readonly string MiddleIsCheckedSettingKeyName = "MiddleIsCheckedSettingKeyName";
        readonly string LastIsCheckedSettingKeyName = "LastIsCheckedSettingKeyName";

        readonly string BeginNotificationNameSettingKeyName = "BeginNotificationNameSettingKeyName";
        readonly string MiddleNotificationNameSettingKeyName = "MiddleNotificationNameSettingKeyName";
        readonly string LastNotificationNameSettingKeyName = "LastNotificationNameSettingKeyName";

        //wartosci domyslne
        DateTime? BeginTimeSettingDefault = DateTime.Now;
        DateTime? MiddleTimeSettingDefault = DateTime.Now.AddHours(6);
        DateTime? LastTimeSettingDefault = DateTime.Now.AddHours(12);
        bool BeginIsCheckedSettingDefault = false;
        bool MiddleIsCheckedSettingDefault = false;
        bool LastIsCheckedSettingDefault = false;
        string BeginNotificationNameSettingDefault = "";
        string MiddleNotificationNameSettingDefault = "";
        string LastNotificationNameSettingDefault = "";

        public STSSettings()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                settings = IsolatedStorageSettings.ApplicationSettings;
            }
        }

        public bool AddOrUpdateValue(string key, Object value)
        {
            bool valueChanged = false;

            if (settings.Contains(key))
            {
                if (settings[key] != value)
                {
                    settings[key] = value;
                    valueChanged = true;
                }
            }
            else
            {
                settings.Add(key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        public T GetValueOrDefault<T>(string key, T defaultValue)
        {
            T value;

            if (settings.Contains(key))
            {
                value = (T)settings[key];
            }
            else
            {
                value = defaultValue;
            }
            return value;
        }

        public void Save(string property)
        {
            settings.Save();
            OnPropertyChanged(property);
        }

        public DateTime? BeginTimeSetting
        {
            get
            {
                return GetValueOrDefault<DateTime?>(BeginTimeSettingKeyName, BeginTimeSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(BeginTimeSettingKeyName, value))
                {
                    Save("BeginTimeSetting");
                }
            }
        }

        public DateTime? MiddleTimeSetting
        {
            get
            {
                return GetValueOrDefault<DateTime?>(MiddleTimeSettingKeyName, MiddleTimeSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(MiddleTimeSettingKeyName, value))
                {
                    Save("MiddleTimeSetting");
                }
            }
        }

        public DateTime? LastTimeSetting
        {
            get
            {
                return GetValueOrDefault<DateTime?>(LastTimeSettingKeyName, LastTimeSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(LastTimeSettingKeyName, value))
                {
                    Save("LastTimeSetting");
                }
            }
        }

        public bool BeginIsCheckedSetting
        {
            get
            {
                return GetValueOrDefault<bool>(BeginIsCheckedSettingKeyName, BeginIsCheckedSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(BeginIsCheckedSettingKeyName, value))
                {
                    Save("BeginIsCheckedSetting");
                }
            }
        }

        public bool MiddleIsCheckedSetting
        {
            get
            {
                return GetValueOrDefault<bool>(MiddleIsCheckedSettingKeyName, MiddleIsCheckedSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(MiddleIsCheckedSettingKeyName, value))
                {
                    Save("MiddleIsCheckedSetting");
                }
            }
        }

        public bool LastIsCheckedSetting
        {
            get
            {
                return GetValueOrDefault<bool>(LastIsCheckedSettingKeyName, LastIsCheckedSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(LastIsCheckedSettingKeyName, value))
                {
                    Save("LastIsCheckedSetting");
                }
            }
        }

        public string BeginNotificationNameSetting
        {
            get
            {
                return GetValueOrDefault<string>(BeginNotificationNameSettingKeyName, BeginNotificationNameSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(BeginNotificationNameSettingKeyName, value))
                {
                    Save("BeginNotificationNameSetting");
                }
            }
        }

        public string MiddleNotificationNameSetting
        {
            get
            {
                return GetValueOrDefault<string>(MiddleNotificationNameSettingKeyName, MiddleNotificationNameSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(MiddleNotificationNameSettingKeyName, value))
                {
                    Save("MiddleNotificationNameSetting");
                }
            }
        }

        public string LastNotificationNameSetting
        {
            get
            {
                return GetValueOrDefault<string>(LastNotificationNameSettingKeyName, LastNotificationNameSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(LastNotificationNameSettingKeyName, value))
                {
                    Save("LastNotificationNameSetting");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
